var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var communityUrl = require("url/community");
var qqHttp = require("helper/http");
var listdata = mm("do_ListData");

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");

header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "选择标签",
	"labelNext" : "确认"
})
header.refreshData();


//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(data) {
	laodPage();
});

var tagList = null;

function laodPage() {
	qqHttp.post(communityUrl.url.message.tagList, {}, function(data) {
		tagList = data.tagList;
		ui("do_GridView_1").items = tagList;
	})
}

do_Page.on("fire_select_tag", function(data, e) {
	for(var i in tagList ){
		if(tagList[i].code == data.code){
			tagList[i].isSelect = data.isSelected;
			break;
		}
	}
})

headerRight.on("touch", function() {
	var tagIds = [];
	var tagNames = [];
	for(var i in tagList ){
		if(tagList[i].isSelect == "1"){
			tagIds.push(tagList[i].id);
			tagNames.push(tagList[i].name);
		}
	}
	deviceone.print(tagNames.join(";"));
	do_App.closePage({"tagIds":tagIds.join(";"),"tagNames":tagNames.join(";")})
})
